package com.yx.coredata.utils;

import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

/**
 * @author cuihao
 * @create 2018-05-30-14:44
 */
public class JsonUtil {
    public static void main(String args[]){
        JSONObject jsonObject = getJSONObject("{d:test,e:true,b:1.1,c:1,a:1}");
        System.out.println(jsonObject);
//{"d":"test","e":true,"b":1.1,"c":1,"a":1}
        try {
            System.out.println(jsonObject.getInt("a"));
            System.out.println(jsonObject.getDouble("b"));
            System.out.println(jsonObject.getLong("c"));
            System.out.println(jsonObject.getString("d"));
            System.out.println(jsonObject.getBoolean("e"));
        } catch (JSONException e) {
            e.printStackTrace();
        }



    }
    public static JSONObject getJSONObject(String str) {
        if (str == null || str.trim().length() == 0) {
            return null;
        }
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject(str);
        } catch (JSONException e) {
            e.printStackTrace(System.err);
        }
        return jsonObject;
    }
}
